<?php

namespace app\common\model\banxi\student;

use app\common\model\banxi\say\SayTeachSentence;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 学生额外信息
 */
class StudentCourse extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_student_course';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;
    protected $append = [

    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'studentId' => 'string',
        'courseId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------
    public function listByStudentId(string $studentId)
    {
        return $this->where(['studentId' => $studentId])->column('courseId');
    }

    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with([
            'courseInfo' => function ($query) {
                $query->with([
                    'gradeInfo' => function ($query) {
                        $query->field('id,name');
                    }])->field('id,gradeId,name,sentenceGroupEn,sentenceGroupZh');
            }])->where($where)->paginate([
            'page' => $page,
            'list_rows' => $limit,
        ]);
    }


    public function getInfo(array $where)
    {
        return $this->with(['courseInfo'])->where($where)->find();
    }

    public function saveOrUpdate(array $data)
    {
        $id = $data['id'] ?? 0;
        if ($id) {
            $this->update($data, ['id' => $id]);
        } else {
            $this->save($data);
        }
        return true;
    }

    public function del(array $ids)
    {
        return self::destroy(function ($query) use ($ids) {
            $query->whereIn('id', $ids);
        });
    }

    public function openCourse(string|int $studentId, array $courseIds)
    {
        $studentCourseList = [];
        $courseRawIds = $this->where(['studentId' => $studentId])->column('courseId');
        foreach ($courseIds as $courseId) {
            if (!in_array($courseId, $courseRawIds)) {
                $studentCourseList[] = [
                    'studentId' => $studentId,
                    'courseId' => $courseId,
                ];
            }
        }
        return $this->saveAll($studentCourseList);
    }



    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------
    public function courseInfo()
    {
        return $this->hasOne(SayTeachSentence::class, 'id', 'courseId');
    }

}